// pages/quanzi/article/article.js
const appConfig = require('../../../config/config.js');
const http = require('../../../utils/http.js');
const nav = require('../../../utils/navigator.js');
const tip = require('../../../utils/tip.js');
var WxParse = require('../../../wxParse/wxParse.js'); 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    authorId:0,//文章发布人id
    articleId:null,
    txtImg: '',
    txtTitle: '', //标题
    txtName: '', //名字
    txtDate:'',
    txtHtml: '', //图文html内容
    txtFavoriteCnt:0,
    hasFavorite: false, //是否关注
    hasFavoriteUser:false,//是否关注用户
    favoriteUserCnt:0,//粉丝数
    replyName: '说点什么...',
    replyUserId: null, //回复某一用户的id
    sourceId: null, //源留言id
    commentContent: '', //留言内容
    msgCnt: '',
    msgList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var articleId = options.articleId;
    console.log(articleId);
    that.setData({
      articleId: articleId
    });
    wx.showLoading({
      title: '玩命加载中...',
    });
    http.post(appConfig.baseUrl + 'Article/GetArticleDetail', {
      Id: articleId,
      UserId: wx.getStorageSync('userId')
    },
      function (res) { //
        console.log(res.data);
        wx.hideLoading(); //关闭加载动画
        if (!res.data) {
          tip.showToast('抱歉，未获取到文章信息', 'none', function (res) {
            setTimeout(function () {
              nav.redirectTo("../../index/index");
            }, 2000);
          });
          return false;
        }
        WxParse.wxParse('article', 'html', res.data.Content, that, 5);
        that.setData({
          txtImg: res.data.AvatarUrl,
          txtTitle: res.data.Title, 
          txtName: res.data.NickName, 
          msgList: res.data.MsgList,
          txtFavoriteCnt: res.data.FavoriteCnt,
          authorId: res.data.UserId,
          favoriteUserCnt: res.data.FavoriteUserCnt,
          txtDate: res.data.CreateTimeName,
          hasFavorite: res.data.HasFavorite>0?true:false,
          hasFavoriteUser: res.data.HasFavoriteUser>0?true:false
          //txtHtml: res.data.Content
        });
    });
  },
  /**
     * 发表留言
     */
  bindMsgClick: function (e) {
    var that = this;
    var content = that.data.commentContent;
    if (content == null || content == '') {
      tip.showToast('亲，您还未输入留言内容哦');
      return false;
    }
    http.post(appConfig.baseUrl + 'Notice/SendMsg', {
      ParentId: that.data.sourceId,
      UserId: wx.getStorageSync('userId'),
      ArticleId: that.data.articleId,
      ReplyUserId: that.data.replyUserId,
      Content: that.data.commentContent
    }, function (res) {
      if (res) {
        console.log(res.data);
        if (res.data.Flag == true) {
          that.setData({
            msgList: res.data.Content,
            commentContent: '',
            replyName: "说点什么...",
            replyUserId: null,
            sourceId: null
          });
          tip.showToast('留言成功');
          //刷新留言数量
          http.post(appConfig.baseUrl + 'Notice/GetMsgCount', {
            ArticleId: that.data.articleId
          }, function (res) {
            console.log(res.data)
            if (res.data.Flag == true) {
              that.setData({ msgCnt: res.data.Content });
            }
          }, function (res) {
            tip.showToast('系统繁忙，请稍后再试！');
          });
        } else {
          tip.showToast(res.data.CodeMsg);
        }

      }
    }, function (res) {
      tip.showToast('系统繁忙，请稍后再试！');
    });
  },
  /**
   * 留言文本框事件
   */
  bindCommentBlur: function (e) {
    this.setData({
      commentContent: e.detail.value,
    });
  },
  /**
   * 留言内容点击事件
   */
  bindReplyClick: function (e) {
    var that = this;
    var name = e.currentTarget.dataset.name;
    var replyUserId = e.currentTarget.dataset.replyuserid;
    console.log(name + replyUserId);
    that.setData({
      replyName: "回复:" + name,
      replyUserId: replyUserId,
      sourceId: e.currentTarget.dataset.sourceid
    });

  },
  /** 
   * 收藏按钮点击事件
   */
  bindFavoriteClick: function (e) {
    var that = this;
    //console.log(that.data.noticeId)
    wx.showLoading({
      title: '加载中，请稍后...',
    });
    http.post(appConfig.baseUrl + 'Article/SaveFavorite', {
      ArticleId: that.data.articleId,
      UserId: wx.getStorageSync('userId')
    }, function (res) {
      if (res) {
        console.log(res.data);
        wx.hideLoading(); //关闭加载动画
        if (res.data.Flag == true) {
          that.setData({
            hasFavorite: res.data.Content == "0" ? false : true,
            txtFavoriteCnt: res.data.Code
          });
        } else {
          tip.showToast('抱歉，处理失败，请稍后再试！');
        }


      }
    }, function (res) {
      wx.hideLoading(); //关闭加载动画
      tip.showToast('系统繁忙，请稍后再试！');
    });
    
  },
  bindReward:function(e){
    tip.showToast('抱歉，打赏功能暂未开放！');
  },
  bindShare: function (e) { 
    tip.showToast('抱歉，分享功能暂未开放！');
  },
  /**
   * 关注用户点击事件
   */
  bindTopClick:function(e){
    var that = this;
    wx.showLoading({
      title: '加载中，请稍后...',
    });
    http.post(appConfig.baseUrl + 'Article/FavoriteUser', {
      AuthorId: that.data.authorId,
      UserId: wx.getStorageSync('userId')
    }, function (res) {
      if (res) {
        console.log(res.data);
        wx.hideLoading(); //关闭加载动画
        if (res.data.Flag == true) {
          that.setData({
            hasFavoriteUser: res.data.Content == "0" ? false : true,
            favoriteUserCnt: res.data.Code
          });
        } else {
          tip.showToast('抱歉，处理失败，请稍后再试！');
        }


      }
    }, function (res) {
      wx.hideLoading(); //关闭加载动画
      tip.showToast('系统繁忙，请稍后再试！');
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})